﻿
Imports osi.root.constants

Namespace error_handle
    Public Class colorful_console_error_writer
        Implements ierror_writer

        Public Shared ReadOnly instance As locked_writer(Of error_type_selected_error_writer) = Nothing

        Shared Sub New()
            instance = New locked_writer(Of error_type_selected_error_writer) _
                                        (New error_type_selected_error_writer(New colorful_console_error_writer()))
            instance.writer.skip_error_type = "pi"
        End Sub

        Private Sub New()
        End Sub

        Public Function [select](ByVal error_type As error_type,
                                 ByVal error_type_char As Char) As Boolean Implements ierror_writer.select
            Return True
        End Function

        Private Sub write(ByVal s As String)
            Console.WriteLine(s)
        End Sub

#If Not (PocketPC OrElse Smartphone) Then
        Private Sub colorful_write(ByVal errtype As error_type, ByVal s As String)
            Dim oldcolor As ConsoleColor = Nothing
            oldcolor = Console.ForegroundColor
            Try
                If errtype = error_type.application Then
                    Console.ForegroundColor = ConsoleColor.Blue
                ElseIf errtype = error_type.critical Then
                    Console.ForegroundColor = ConsoleColor.Red
                ElseIf errtype = error_type.exclamation Then
                    Console.ForegroundColor = ConsoleColor.Yellow
                ElseIf errtype = error_type.information Then
                    Console.ForegroundColor = ConsoleColor.White
                ElseIf errtype = error_type.other Then
                    Console.ForegroundColor = ConsoleColor.Magenta
                ElseIf errtype = error_type.user Then
                    Console.ForegroundColor = ConsoleColor.Cyan
                ElseIf errtype = error_type.system Then
                    Console.ForegroundColor = ConsoleColor.Gray
                ElseIf errtype = error_type.performance Then
                    Console.ForegroundColor = ConsoleColor.DarkYellow
                Else
                    Console.ForegroundColor = ConsoleColor.Green
                End If
                write(s)
            Finally
                Console.ForegroundColor = oldcolor
            End Try
        End Sub
#End If

        Public Sub write(ByVal errtype As error_type, ByVal s As String) Implements ierror_writer.write
#If PocketPC OrElse Smartphone Then
            write(s)
#Else
            colorful_write(errtype, s)
#End If
        End Sub
    End Class
End Namespace
